package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.BaseAttrInfo;
import com.atguigu.gmall.product.entity.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "平台属性")
@RestController
@RequestMapping("/admin/product")
public class BaseAttrController {

    @Autowired
    BaseAttrInfoService baseAttrInfoService;
    @Autowired
    BaseAttrValueService baseAttrValueService;

    //   /admin/product/attrInfoList/{category1Id}/{category2Id}/{category3Id}
    @ApiOperation("根据分类id获取平台属性")
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result getBaseAttr(@PathVariable("category1Id") Long category1Id,
                              @PathVariable("category2Id") Long category2Id,
                              @PathVariable("category3Id") Long category3Id){
        List<BaseAttrInfo> attrInfos = baseAttrInfoService.getBaseAttrAndValue(category1Id,category2Id,category3Id);
        return Result.ok(attrInfos);
    }

    //  http://api.gmall.com/admin/product/saveAttrInfo
    @ApiOperation("添加/修改  平台属性")
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        if (baseAttrInfo.getId()==null){
            //添加保存
            baseAttrInfoService.saveAttrInfo(baseAttrInfo);//操作表base_attr_info  base_attr_value
        } else {
            //修改
            baseAttrInfoService.updateAttrInfo(baseAttrInfo);//操作表base_attr_info  base_attr_value
        }
        return Result.ok();
    }

    //  /admin/product/getAttrValueList/{attrId}
    @ApiOperation("根据平台属性ID获取平台属性")
    @GetMapping("/getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable("attrId") Long attrId){
        List<BaseAttrValue> baseAttrValues= baseAttrValueService.getAttrValueList(attrId);
        return Result.ok(baseAttrValues);
    }
}
